﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zen4SyncDashboard.DAL;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace Zen4SyncDashboard.ViewModel
{
    public class CreateTestSessionViewModel : BaseViewModel
    {
        #region - Bound Properties -

        public testSession NewTestSession { get; private set; }

        public ObservableCollection<testSessionCategory> TestSessionCategories { get; private set; }
        public ObservableCollection<testServer> TestServers { get; private set; }
        public ObservableCollection<testScenario> TestScenarios { get; private set; }

        public clientSession NewClientSession { get; private set; }

        #endregion - Bound Properties -

        #region - Commands -

        public RelayCommand AddNewTestSessionCommand { get; private set; }
        public RelayCommand AddNewClientSessionCommand { get; private set; }

        #endregion - Commands -

        public CreateTestSessionViewModel()
        {
            InitializeBoundProperties();
            InitializeCommands();
        }

        #region - Initialization Methods -

        private void InitializeBoundProperties()
        {
            NewTestSession = Zen4SyncModel.TestSession.Create();
            NewClientSession = Zen4SyncModel.ClientSession.Create();

            Task.Factory
                .StartNew(() =>
                {
                    base.SetWorkStatus("Fetching Test Session Categories...");
                    TestSessionCategories = Zen4SyncModel.TestSessionCategory.GetAll();
                    base.OnPropertyChanged("TestSessionCategories");

                    base.SetWorkStatus("Fetching Test Servers...");
                    TestServers = Zen4SyncModel.TestServer.GetAll();
                    base.OnPropertyChanged("TestServers");

                    base.SetWorkStatus("Fetching Test Scenarios...");
                    TestScenarios = Zen4SyncModel.TestScenario.GetAll();
                    base.OnPropertyChanged("TestScenarios");

                    base.SetWorkStatus(false);
                });
        }

        private void InitializeCommands()
        {
            AddNewTestSessionCommand = new RelayCommand(
                param => AddNewTestSession(),
                param => CanAddNewTestSession());

            AddNewClientSessionCommand = new RelayCommand(
                param => AddNewClientSession(),
                param => CanAddNewClientSession());
        }        

        #endregion - Initialization Methods -

        private bool CanAddNewClientSession()
        {
            //return NewClientSession.IsValid();
            return true;
        }

        private void AddNewClientSession()
        {
            // Re-creating all the contexts of the clients using the bound properties.
            foreach (activity act in NewClientSession.testScenario.activities)
            {
                activityContext context = Zen4SyncModel.ActivityContext.Create();
                context.activity = act;
                context.activityContext_contextItemId = act.CurrentContextItemId;
                context.activityContext_clientSessionId = NewClientSession.clientSession_id;

                NewClientSession.activityContexts.Add(context);
            }

            NewTestSession.clientSessions.Add(NewClientSession);

            NewClientSession = Zen4SyncModel.ClientSession.Create();
            base.OnPropertyChanged("NewClientSession");
        }

        private bool CanAddNewTestSession()
        {
            //return NewTestSession.IsValid();
            return true;
        }

        private void AddNewTestSession()
        {
            Zen4SyncModel.TestSession.Add(NewTestSession);

            NewTestSession = Zen4SyncModel.TestSession.Create();
            base.OnPropertyChanged("NewTestSession");
        }
    }
}
